let db = require("../db/index");
//存储住户确认表中的信息
let confirmList = []
// 获取当前日期
const date = new Date();

// 提取年份、月份和日期
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以需要加1，并且用padStart确保始终是两位数
const day = String(date.getDate()).padStart(2, '0'); // 用padStart确保日期始终是两位数

//所有的信息
exports.all = (req, res) => {
    var sql =
        'SELECT RoomID,BuildingName,UnitName,FloorNumber,RoomNumber,Area,`Status`,`Usage`,`Owner`,Phone,Type,TypeID,CardNum,AddDate,AddMan,Islink from building, unit,floor,room where building.BuildingID=unit.BuildingID AND unit.UnitID=floor.UnitID and floor.FloorID=room.FloorID';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//更新住户的状态停用和启用
exports.updateStatus = (req, res) => {
    var sql = `update room set IsLink='${req.body.str}' where RoomID=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//更新用户数据
exports.updateAll = (req, res) => {
    var sql = `update room set Owner='${req.body.name}',TypeID='${req.body.cardType}',CardNum='${req.body.cardID}',Type='${req.body.ownerType}',Phone='${req.body.phoneNum}' where RoomID=${req.body.RoomID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//住户确认
exports.confirm = (req, res) => {
    var sql =
        'select * from residentconfirm';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
        confirmList = data
    });
};
//住户确认的数据修改
exports.confirmEdit = (req, res) => {
    let editID
    let filterData = confirmList.filter(item => item.ID == req.body.id)
    console.log(req.body);
    
    //如果传过来的是已通过
    if (req.body.str == '已通过') {
        var sql = `update residentconfirm set State='${req.body.str}' where ID=${req.body.id}`;
        db.query(sql, [], (err, data) => {
            if (err) {
                console.log("出错了", err);
                return res.send("错误" + err.message);
            }

        });
        console.log(`SELECT  RoomID from building, unit,floor,room where building.BuildingID=unit.BuildingID AND unit.UnitID=floor.UnitID and floor.FloorID=room.FloorID and building.BuildingName='${filterData[0].BuildingName}'and unit.UnitName='${filterData[0].UnitName}' and room.RoomNumber='${filterData[0].RoomNumber}'`, '断点');

        var sql = `SELECT  RoomID from building, unit,floor,room where building.BuildingID=unit.BuildingID AND unit.UnitID=floor.UnitID and floor.FloorID=room.FloorID and building.BuildingName='${filterData[0].BuildingName}'and unit.UnitName='${filterData[0].UnitName}' and room.RoomNumber='${filterData[0].RoomNumber}'`;
        db.query(sql, [], (err, data) => {
            if (err) {
                console.log("出错了", err);
                return res.send("错误" + err.message);
            }
            //拿到需要修改的ID
            console.log(data[0].RoomID, 8888);

            editID = data[0].RoomID
            var sql = `update room set Status='已入住',Owner='${filterData[0].Owner}',Type='${filterData[0].OwnerType}',TypeID='${filterData[0].CardType}',CardNum='${filterData[0].CardNum}',Phone='${filterData[0].Phone}',AddMan='${req.body.addMan}',IsLink='启用',AddDate='${year}-${month}-${day}' where RoomID=${editID}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send({
                    status: 500
                });
            });
        });
        //拿到指定的数据
        // let filterData=confirmList.filter(item=>item.ID==req.body.id)
        // console.log(filterData[0].ID,77777);
        // console.log(editID,77888);



    } else {
        console.log(12345641);
        
        if (req.body.resource != '其他') {
            var sql = `update residentconfirm set State='${req.body.str}',Remark='${req.body.resource}' where ID=${req.body.id}`;
            console.log(sql,88888);
            
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }

            });
        } else {
            var sql = `update residentconfirm set State='${req.body.str}',Remark='${req.body.remark}' where ID=${req.body.id}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }

            });
        }
        res.send({
            status: 500
        });
    }
};
// //更新
// exports.update = (req, res) => {
//     var sql =
//         "update t_user set name = ?,mobile=?,idNo=?,sex=?,noteText=?,groupId=? where id = ?";
//     db.query(
//         sql,
//         [
//             req.body.name,
//             req.body.mobile,
//             req.body.idNo,
//             req.body.sex,
//             req.body.noteText,
//             req.body.groupId,
//             req.body.id,
//         ],
//         (err, data) => {
//             if (err) {
//                 console.log("出错了", err);
//                 return res.send("错误" + err);
//             }
//             res.send(data);
//         }
//     );
// };